﻿
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Core;
using System.Web.UI.HtmlControls;
using System.Configuration;
using System.IO;

namespace Yuanlin.Business
{
    public class CompanyGreenNumEditControl : YuanlinUserControl,
        IReplyConverter<CompanyGreenNumReply, GetOperation>
    {
        protected ValuedTextBox greenNumValuedTextBox;
        protected IButton SaveButton;
        protected IButton BackButton;
        private CompanyGreenNum companyGreenNum;
        public int companygreennumid = UrlManager.Instance.GetParameterValue<int>(YuanlinParameterName.CompanyGreenNumId);

        public override void GetRequests(List<BaseRequestBinder> requestBinders)
        {
            requestBinders.Add(RequestHelper.BuildGetCompanyGreenNumRequest(this));
        }
        protected override void OnLoad(EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                Bind();
            }
            SaveButton.Click += new EventHandler(SaveButton_Click);
            BackButton.Click += new EventHandler(BackButton_Click);
        }

        private void Bind()
        {
            if (companyGreenNum != null)
            {
                this.greenNumValuedTextBox.Value = companyGreenNum.GreenNum.Value;
            }
        }

        public void BackButton_Click(object sender, EventArgs e)
        {
            PageRedirect();
        }
        public void SaveButton_Click(object sender, EventArgs e)
        {
            if (companyGreenNum != null)
            {
                if (companyGreenNum.GreenNum.Value == this.greenNumValuedTextBox.Value.Trim())
                {
                    Globals.ShowMessage(Page, "CompanyGreenNumberHasNotBeenModified", "您没有对绿商号做修改，请对其进行修改后再保存！");
                    return;
                }
                CompanyGreenNumListRequest request = new CompanyGreenNumListRequest();
                request.GreenNum = this.greenNumValuedTextBox.Value.Trim();
                EntityList companyGreenNums = BusinessManager.GetCompanyGreenNumList(RequestHelper.BuildGetCompanyGreenNumListRequest(request));
                if (companyGreenNums.Count > 0 && companyGreenNums[0].EntityId.Value != companygreennumid)
                {
                    Globals.ShowMessage(Page, "CompanyGreenNumberExist", "修改后的绿商号已经存在，请更新为其他的绿商号！");
                    return;
                }

                companyGreenNum.GreenNum.Value = this.greenNumValuedTextBox.Value;

                BusinessManager.UpdateCompanyGreenNum(companyGreenNum);
            }
            PageRedirect();
        }
        private void PageRedirect()
        {
            Page.Response.Redirect(SiteUrls.Instance().GetCompanyGreenNumListUrl());
        }

        #region IReplyConverter<CompanyGreenNumReply,GetOperation> Members

        public void ConvertReply(CompanyGreenNumReply reply, GetOperation operation)
        {
            if (reply != null)
            {
                companyGreenNum = reply.Entity as CompanyGreenNum;

                if (!Page.IsPostBack)
                {
                    Bind();
                }
            }
        }

        #endregion
    }
}
